package ltd.hanamaki.login;

import ltd.hanamaki.util.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

public class EcShopLoginTest {
  private WebDriver driver;
  @BeforeClass
  public void setUp() {
    System.setProperty("webdriver.gecko.driver", "C:\\Program Files\\Mozilla Firefox\\geckodriver.exe");
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test(dataProvider = "login_file" )
  public void ecShopLogin(String username,String password) {
    driver.get("http://192.168.0.142/ecshop/");
    driver.manage().window().maximize();
    if (driver.getPageSource().contains("退出")) {
      System.out.println("在登陆 ，请先退出 在注册");
      driver.findElement(By.linkText("退出")).click();
    } else {
      System.out.println("继续 走流程");
    }
    driver.findElement(By.cssSelector("#ECS_MEMBERZONE > a:nth-child(2) > img")).click();
    driver.findElement(By.name("username")).sendKeys(username);
    driver.findElement(By.name("password")).sendKeys(password);
    driver.findElement(By.name("submit")).click();

    /* 断言来 判断登录是否成功 */
    String pageSource = driver.getPageSource();
    boolean b = pageSource.contains("退出");
    if (b) {
      System.out.println("登陆成功");
    } else {
      System.out.println("登陆失败");
    }

    Assert.assertTrue(b);

  }

  @DataProvider
  public Object[][] login_file() {
    return FileUtils.getFileContent("src/main/java/ltd/hanamaki/login/login.csv");
  }

  @AfterClass
  public void tearDown() {
    driver.quit();
  }


}
